home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / man / cat.1 / perlstyle.1 < prev    next >
Text File  |  1995-07-25  |  9KB  |  265 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.           perlstyle - Perl style guide
  10.  
  11.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.           SSSSttttyyyylllleeee
  13.  
  14.           Each programmer will, of course, have his or her own
  15.           preferences in regards to formatting, but there are some
  16.           general guidelines that will make your programs easier to
  17.           read, understand, and maintain.
  18.  
  19.           Regarding aesthetics of code lay out, about the only thing
  20.           Larry cares strongly about is that the closing curly brace
  21.           of a multi-line BLOCK should line up with the keyword that
  22.           started the construct.  Beyond that, he has other
  23.           preferences that aren't so strong:
  24.  
  25.           o+   4-column indent.
  26.  
  27.           o+   Opening curly on same line as keyword, if possible,
  28.               otherwise line up.
  29.  
  30.           o+   Space before the opening curly of a multiline BLOCK.
  31.  
  32.           o+   One-line BLOCK may be put on one line, including
  33.               curlies.
  34.  
  35.           o+   No space before the semicolon.
  36.  
  37.           o+   Semicolon omitted in "short" one-line BLOCK.
  38.  
  39.           o+   Space around most operators.
  40.  
  41.           o+   Space around a "complex" subscript (inside brackets).
  42.  
  43.           o+   Blank lines between chunks that do different things.
  44.  
  45.           o+   Uncuddled elses.
  46.  
  47.           o+   No space between function name and its opening paren.
  48.  
  49.           o+   Space after each comma.
  50.  
  51.           o+   Long lines broken after an operator (except "and" and
  52.               "or").
  53.  
  54.           o+   Space after last paren matching on current line.
  55.  
  56.           o+   Line up corresponding items vertically.
  57.  
  58.           o+   Omit redundant punctuation as long as clarity doesn't
  59.               suffer.
  60.  
  61.  
  62.  
  63.      Page 1                                          (printed 6/30/95)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))
  71.  
  72.  
  73.  
  74.           Larry has his reasons for each of these things, but he
  75.           doen't claim that everyone else's mind works the same as his
  76.           does.
  77.  
  78.           Here are some other more substantive style issues to think
  79.           about:
  80.  
  81.           o+   Just because you _C_A_N do something a particular way
  82.               doesn't mean that you _S_H_O_U_L_D do it that way.  Perl is
  83.               designed to give you several ways to do anything, so
  84.               consider picking the most readable one.  For instance
  85.  
  86.                   open(FOO,$foo) || die "Can't open $foo: $!";
  87.  
  88.               is better than
  89.  
  90.                   die "Can't open $foo: $!" unless open(FOO,$foo);
  91.  
  92.               because the second way hides the main point of the
  93.               statement in a modifier.  On the other hand
  94.  
  95.                   print "Starting analysis\n" if $verbose;
  96.  
  97.               is better than
  98.  
  99.                   $verbose && print "Starting analysis\n";
  100.  
  101.               since the main point isn't whether the user typed ----vvvv or
  102.               not.
  103.  
  104.               Similarly, just because an operator lets you assume
  105.               default arguments doesn't mean that you have to make use
  106.               of the defaults.  The defaults are there for lazy
  107.               systems programmers writing one-shot programs.  If you
  108.               want your program to be readable, consider supplying the
  109.               argument.
  110.  
  111.               Along the same lines, just because you _C_A_N omit
  112.               parentheses in many places doesn't mean that you ought
  113.               to:
  114.  
  115.                   return print reverse sort num values %array;
  116.                   return print(reverse(sort num (values(%array))));
  117.  
  118.               When in doubt, parenthesize.  At the very least it will
  119.               let some poor schmuck bounce on the % key in vvvviiii.
  120.  
  121.               Even if you aren't in doubt, consider the mental welfare
  122.               of the person who has to maintain the code after you,
  123.               and who will probably put parens in the wrong place.
  124.  
  125.  
  126.  
  127.  
  128.  
  129.      Page 2                                          (printed 6/30/95)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))
  137.  
  138.  
  139.  
  140.           o+   Don't go through silly contortions to exit a loop at the
  141.               top or the bottom, when Perl provides the last operator
  142.               so you can exit in the middle.  Just "outdent" it a
  143.               little to make it more visible:
  144.  
  145.                   LINE:
  146.                       for (;;) {
  147.                           statements;
  148.                         last LINE if $foo;
  149.                           next LINE if /^#/;
  150.                           statements;
  151.                       }
  152.  
  153.  
  154.           o+   Don't be afraid to use loop labels--they're there to
  155.               enhance readability as well as to allow multi-level loop
  156.               breaks.  See the previous example.
  157.  
  158.           o+   For portability, when using features that may not be
  159.               implemented on every machine, test the construct in an
  160.               eval to see if it fails.  If you know what version or
  161.               patchlevel a particular feature was implemented, you can
  162.               test $] ($PERL_VERSION in English) to see if it will be
  163.               there.  The Config module will also let you interrogate
  164.               values determined by the CCCCoooonnnnffffiiiigggguuuurrrreeee program when Perl was
  165.               installed.
  166.  
  167.           o+   Choose mnemonic identifiers.  If you can't remember what
  168.               mnemonic means, you've got a problem.
  169.  
  170.           o+   If you have a really hairy regular expression, use the
  171.               /x modifier and put in some whitespace to make it look a
  172.               little less like line noise.  Don't use slash as a
  173.               delimiter when your regexp has slashes or backslashes.
  174.  
  175.           o+   Use the new "and" and "or" operators to avoid having to
  176.               parenthesize list operators so much, and to reduce the
  177.               incidence of punctuational operators like && and ||.
  178.               Call your subroutines as if they were functions or list
  179.               operators to avoid excessive ampersands and parens.
  180.  
  181.           o+   Use here documents instead of repeated _p_r_i_n_t()
  182.               statements.
  183.  
  184.           o+   Line up corresponding things vertically, especially if
  185.               it'd be too long to fit on one line anyway.
  186.  
  187.                   $IDX = $ST_MTIME;
  188.                   $IDX = $ST_ATIME       if $opt_u;
  189.                   $IDX = $ST_CTIME       if $opt_c;
  190.                   $IDX = $ST_SIZE        if $opt_s;
  191.  
  192.  
  193.  
  194.  
  195.      Page 3                                          (printed 6/30/95)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))PPPPEEEERRRRLLLLSSSSTTTTYYYYLLLLEEEE((((1111))))
  203.  
  204.  
  205.  
  206.                   mkdir $tmpdir, 0700 or die "can't mkdir $tmpdir: $!";
  207.                   chdir($tmpdir)      or die "can't chdir $tmpdir: $!";
  208.                   mkdir 'tmp',   0777 or die "can't mkdir $tmpdir/tmp: $!";
  209.  
  210.  
  211.           o+   Line up your translations when it makes sense:
  212.  
  213.                   tr [abc]
  214.                      [xyz];
  215.  
  216.  
  217.           o+   Think about reusability.  Why waste brainpower on a
  218.               one-shot when you might want to do something like it
  219.               again?  Consider generalizing your code.  Consider
  220.               writing a module or object class.  Consider making your
  221.               code run cleanly with use strict and ----wwww in effect.
  222.               Consider giving away your code.  Consider changing your
  223.               whole world view.  Consider... oh, never mind.
  224.  
  225.           o+   Be consistent.
  226.  
  227.           o+   Be nice.
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.      Page 4                                          (printed 6/30/95)
  262.  
  263.  
  264.  
  265.